package zhishuang.wang.sansi.playlist.x9;

import zhishuang.wang.sansi.playlist.*;
import zhishuang.wang.sansi.playlist.entry.*;
import zhishuang.wang.sansi.tools.ReturnData;
import zhishuang.wang.sansi.tools.json.exception.FastjsonUtil;
import zhishuang.wang.sansi.tools.time.DateFormat;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 11:11
 * @Email 1406110602@qq.com
 */

public class PlayListX9 {
    public PlayListX9() {
    }

    private Map videoTimeRangeMapSet(PlayArea pa) {
        if (pa != null && pa.getValidFlag() != null && pa.getValidFlag() == 1) {
            Map map = new HashMap();
            map.put("start", pa.getStartPosition());
            map.put("end", pa.getEndPosition());
            map.put("enable", "true");
            return map;
        } else {
            return null;
        }
    }

    private Map transitionMapSet(Animation am, int flag) {
        if (am != null) {
            if (flag == 0) {
                if (am.getInAnimation() != null && am.getInAnimationSpeed() != null) {
                    Map map = new HashMap();
                    map.put("type", am.getInAnimation());
                    map.put("speed", am.getInAnimation());
                    return map;
                }
            } else if (flag == 1) {
            }
        }

        return null;
    }

    private Map fontMapSet(TextBase tb) {
        Map map = new HashMap();
        if (tb.getFontName() != null) {
            map.put("name", tb.getFontName());
        }

        if (tb.getFontSize() != null) {
            map.put("size", tb.getFontSize());
        }

        if (tb.getFontColour() != null) {
            BaseColour backColour = tb.getFontColour();
            String bakcColourStr = String.format("%d,%d,%d,%d,%d", backColour.getRed(), backColour.getGreen(), backColour.getBlue(), backColour.getAlpha(), backColour.getAmber());
            map.put("color", bakcColourStr);
        }

        return map;
    }

    private Map backgroundSet(Background background) {
        if (background == null) {
            return null;
        } else {
            Map map = new HashMap();
            if (background.getTransparentFlag() != null) {
                map.put("transparent", background.getTransparentFlag());
            }

            if (background.getBackColour() != null) {
                BaseColour backColour = background.getBackColour();
                String bakcColourStr = String.format("%d,%d,%d,%d,%d", backColour.getRed(), backColour.getGreen(), backColour.getBlue(), backColour.getAlpha(), backColour.getAmber());
                map.put("back_color", bakcColourStr);
            }

            if (background.getBackPicture() != null) {
                PictureBase pb = background.getBackPicture();
                map.put("back_image", pb.getPicturePath());
                if (pb.getShowMode() != null) {
                    map.put("show_mode", pb.getShowMode());
                }
            }

            return map;
        }
    }

    private Map durationSet(PlayTimeBase playTime) {
        Map map = new HashMap();
        if (playTime.getTotalTime() != null) {
            map.put("total", playTime.getTotalTime());
        }

        if (playTime.getPlayNum() != null) {
            map.put("play_count", playTime.getPlayNum());
        }

        if (playTime.getStopTime() != null) {
            map.put("delay", playTime.getStopTime());
        }

        return map;
    }

    private List<Map> showContentSet(List<PlayItem> itemList, Background background) {
        List<Map> contentListMap = new ArrayList();
        Map backgroundMap = this.backgroundSet(background);

        for(int i = 0; i < itemList.size(); ++i) {
            PlayItem item = (PlayItem)itemList.get(i);
            Map itemMap = new HashMap();
            if (backgroundMap != null) {
                itemMap.put("BackGround", backgroundMap);
            }

            Map transitionMap = this.transitionMapSet(item.getAnimation(), 0);
            if (transitionMap != null) {
                itemMap.put("Transition", transitionMap);
            }

            if (item.getPlayTime() != null) {
                itemMap.put("Duration", this.durationSet(item.getPlayTime()));
            }

            Map contentMap = new HashMap();
            switch (item.getMaterialType()) {
                case 1:
                    TextBase textBase = item.getTextBase();
                    itemMap.put("type", 0);
                    switch (textBase.getTextType()) {
                        case 0:
                            contentMap.put("text", textBase.getTextPath());
                            break;
                        case 1:
                            contentMap.put("file", textBase.getTextPath());
                    }

                    if (textBase.getAudit() != null) {
                        itemMap.put("audit", textBase.getAudit());
                    }

                    if (textBase.getShowFlag() != null) {
                        itemMap.put("align", textBase.getShowFlag());
                    }

                    if (textBase.getWordSpace() != null) {
                        itemMap.put("fspace", textBase.getWordSpace());
                    }

                    if (textBase.getLineSpace() != null) {
                        itemMap.put("lspace", textBase.getLineSpace());
                    }

                    Map fontMap = this.fontMapSet(textBase);
                    if (!fontMap.isEmpty()) {
                        itemMap.put("Font", fontMap);
                    }
                    break;
                case 2:
                    PictureBase pictureBase = item.getPictureBase();
                    if (pictureBase.getPictureType() == 2) {
                        itemMap.put("type", 16);
                        contentMap.put("url", pictureBase.getPicturePath());
                        if (pictureBase.getUpdateCycle() != null) {
                            contentMap.put("update", pictureBase.getUpdateCycle());
                        }
                    } else {
                        itemMap.put("type", 3);
                        contentMap.put("file", pictureBase.getPicturePath());
                    }

                    if (pictureBase.getAudit() != null) {
                        itemMap.put("audit", pictureBase.getAudit());
                    }

                    if (pictureBase.getShowMode() != null) {
                        itemMap.put("show_mode", pictureBase.getShowMode());
                    }
                    break;
                case 3:
                    VideoBase videoBase = item.getVideoBase();
                    itemMap.put("type", 4);
                    if (videoBase.getAudit() != null) {
                        itemMap.put("audit", videoBase.getAudit());
                    }

                    if (videoBase.getZoom() != null) {
                        itemMap.put("zoom", videoBase.getZoom());
                    }

                    if (videoBase.getVolume() != null) {
                        itemMap.put("volume", videoBase.getVolume());
                    }

                    Map videoTimeRangeMap = this.videoTimeRangeMapSet(videoBase.getPlayArea());
                    if (videoTimeRangeMap != null) {
                        itemMap.put("TimeRange", videoTimeRangeMap);
                    }

                    contentMap.put("file", videoBase.getVideoPath());
            }

            itemMap.put("Content", contentMap);
            contentListMap.add(itemMap);
        }

        return contentListMap;
    }

    private List<Map> regionsSet(List<AreaItem> areaItemList) {
        new HashMap();
        List<Map> contentListMap = new ArrayList();

        for(int i = 0; i < areaItemList.size(); ++i) {
            AreaItem areaItem = (AreaItem)areaItemList.get(i);
            Map areaMap = new HashMap();
            areaMap.put("name", areaItem.getName());
            areaMap.put("id", areaItem.getAreaPositon().getAreaIndex());
            areaMap.put("x", areaItem.getAreaPositon().getX());
            areaMap.put("y", areaItem.getAreaPositon().getY());
            areaMap.put("width", areaItem.getAreaPositon().getWidth());
            areaMap.put("height", areaItem.getAreaPositon().getHeight());
            areaMap.put("last_frame", 1);
            List<Map> playItemsMap = this.showContentSet(areaItem.getItemList(), areaItem.getBackground());
            Map itemsMap = new HashMap();
            itemsMap.put("Contents", playItemsMap);
            areaMap.put("Items", itemsMap);
            contentListMap.add(areaMap);
        }

        return contentListMap;
    }

    private Map programSceneSet(List<PageItem> pageItemList) {
        Map map = new HashMap();
        List<Map> sceneMapList = new ArrayList();

        for(int i = 0; i < pageItemList.size(); ++i) {
            PageItem pageItem = (PageItem)pageItemList.get(i);
            Map sceneMap = new HashMap();
            sceneMap.put("type", 0);
            sceneMap.put("name", pageItem.getName());
            sceneMap.put("duration", "");
            Map regionsMap = new HashMap();
            List<Map> regionsListMap = this.regionsSet(pageItem.getAreaItemList());
            regionsMap.put("Contents", regionsListMap);
            sceneMap.put("Regions", regionsMap);
            sceneMapList.add(sceneMap);
        }

        Map scenesContentsMap = new HashMap();
        scenesContentsMap.put("Contents", sceneMapList);
        map.put("Scenes", scenesContentsMap);
        return map;
    }

    private Map playTableSet(PlayPlan playPlan) {
        Map map = new HashMap();
        map.put("type", 0);
        map.put("name", playPlan.getName());
        TimePlan timePlan = playPlan.getTimePlan();
        if (timePlan != null) {
            Map dateRangeMap = new HashMap();
            Map timeRangeMap = new HashMap();
            if (timePlan.getValidFlag() != null && timePlan.getValidFlag() == 0) {
                dateRangeMap.put("enable", "false");
                timeRangeMap.put("enable", "false");
            } else {
                dateRangeMap.put("enable", "true");
                timeRangeMap.put("enable", "true");
            }

            String endTime;
            if (timePlan.getStartDate() != null) {
                endTime = timePlan.getStartDate();
                dateRangeMap.put("start", endTime.replaceAll("-", ","));
            } else {
                dateRangeMap.put("start", "2020,01,01");
            }

            if (timePlan.getEndDate() != null) {
                endTime = timePlan.getEndDate();
                dateRangeMap.put("end", endTime.replaceAll("-", ","));
            } else {
                dateRangeMap.put("end", "3000,01,01");
            }

            if (timePlan.getStartTime() != null) {
                endTime = timePlan.getStartTime();
                endTime = endTime + ".000";
                timeRangeMap.put("start", endTime);
            } else {
                timeRangeMap.put("start", "00:00:00.000");
            }

            if (timePlan.getEndTime() != null) {
                endTime = timePlan.getEndTime();
                endTime = endTime + ".000";
                timeRangeMap.put("end", endTime);
            } else {
                timeRangeMap.put("end", "24:00:00.000");
            }

            if (timePlan.getWeekFlag() != null) {
                map.put("DayOfWeek", timePlan.getWeekFlag());
            } else {
                map.put("DayOfWeek", 127);
            }

            if (timePlan.getMonthFlag() != null) {
                map.put("DayOfMonth", timePlan.getMonthFlag());
            } else {
                map.put("DayOfWeek", Integer.MAX_VALUE);
            }

            map.put("DateRange", dateRangeMap);
            map.put("TimeRange", timeRangeMap);
        }

        Map programMap = this.programSceneSet(playPlan.getPageItemList());
        List<Map> programListMap = new ArrayList();
        Map pMap = new HashMap();
        programListMap.add(programMap);
        pMap.put("Contents", programListMap);
        map.put("Programs", pMap);
        return map;
    }

    private Map playProjectSet(PlayList playList) {
        Map map = new HashMap();
        map.put("encoding", "UTF-8");
        map.put("file_type", "xstudiopro_playproject");
        map.put("version", "1");
        List<Map> playTableList = new ArrayList();

        for(int i = 0; i < playList.getPlayPlan().size(); ++i) {
            PlayPlan pp = (PlayPlan)playList.getPlayPlan().get(i);
            Map programMap = this.playTableSet(pp);
            playTableList.add(programMap);
        }

        Map ptMap = new HashMap();
        ptMap.put("Contents", playTableList);
        map.put("PlayTables", ptMap);
        return map;
    }

    public ReturnData createXstudioPlayList(String filePath, PlayList playList) {
        ReturnData rd = new ReturnData();
        ReturnData checkRd = playList.checkParam();
        if (checkRd.getCode() != 0) {
            try {
                Map playListMap = this.playProjectSet(playList);
                String playListStr = FastjsonUtil.to(playListMap);
                System.out.println(playListStr);
                File file = new File(filePath);
                if (file.exists()) {
                    rd.setCode(1);
                    rd.setMessage("播放表存储名称重复");
                } else {
                    Writer w = null;

                    try {
                        file.createNewFile();
                        w = new FileWriter(file);
                        w.write(playListStr);
                        rd.setCode(0);
                        rd.setMessage("生成播放表成功");
                    } catch (IOException var10) {
                        var10.printStackTrace();
                        rd.setCode(1);
                        rd.setMessage("创建播放表文件异常");
                    }

                    if (w != null) {
                        w.close();
                    }

                    if (rd.getCode() != 0) {
                        file.delete();
                    }
                }
            } catch (Exception var11) {
                rd.setCode(1);
                rd.setMessage("生成播放表异常");
            }
        } else {
            rd.setCode(1);
            rd.setMessage(checkRd.getMessage());
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }
}
